<?php
include __DIR__ . "/config/cors.php";

$url = @$_GET['url'];
$role = @$_COOKIE['role'];

if (!isset($url)) {
    echo json_encode([
        "code" => 201,
        "message" => "文件删除失败"
    ]);
    exit;
}
if (!isset($role) or $role != "admin") {
    echo json_encode([
        "code" => 401,
        "message" => "权限不足！"
    ]);
    exit;
}

$local_path = __DIR__ . "/upload/";
$file = basename($url);
$need = $local_path . $file;
// file_exists 判断文件是否存在
// unlink 删除文件
if (file_exists($need)) {
    unlink($need);
    echo json_encode([
        "code" => 200,
        "message" => "文件删除成功"
    ]);
} else {
    echo json_encode([
        "code" => 202,
        "message" => "文件删除失败"
    ]);
}
